import React, { PureComponent } from 'react';
import { Card } from 'antd';
import DescriptionList from '@/components/DescriptionList';
import downloadFile, { getFileName } from '@/utils/utils';

const { Description } = DescriptionList;
class MatInfor extends PureComponent {
  downLoad = item => {
    const file = item.split('|');
    downloadFile({ fileId: file[0], fileName: getFileName(file[1]) });
  };

  addLink = matinfo =>
    matinfo.split(',').map(item => {
      const matinfoItem = item.split('|')[1];
      const matinfoName = getFileName(matinfoItem);
      return (
        <div key={item.split('|')[0]}>
          <a key={item.matId} onClick={() => this.downLoad(item)}>
            {matinfoName}
          </a>
        </div>
      );
    });

  commanName = item => (item.inpVal ? `${item.inpVal}` : '--');

  getVal = value => {
    if (value.inpVal || (value.inpType === 9 && value.inpVal === '[]')) {
      if (value.inpType === 9 && value.inpVal !== '[]') {
        return this.addLink(value.inpVal);
      }
      return this.commanName(value);
    }
    return '--';
  };

  render() {
    const { matSubs, persion } = this.props;
    return (
      <div>
        <Card
          title={
            <span style={{ fontWeight: 'bolder' }} id="materialInfor">
              材料信息
            </span>
          }
          bordered={false}
        >
          {String(persion.coltFlag) === '1' ? (
            <DescriptionList>
              {matSubs.map(value => (
                <Description span={8} term={value.matName}>
                  {this.getVal(value)}
                </Description>
              ))}
              <Description term="材料是否完整">
                {persion.matStateName ? persion.matStateName : '--'}
              </Description>
            </DescriptionList>
          ) : (
            <DescriptionList>
              <Description term="材料是否完整">不需要材料</Description>
            </DescriptionList>
          )}
        </Card>
      </div>
    );
  }
}

export default MatInfor;
